package com.itany.netclass.service.impl;

import com.itany.netclass.constant.GoldPointsConstant;
import com.itany.netclass.constant.UserConstant;
import com.itany.netclass.dao.GoldPointsMapper;
import com.itany.netclass.dao.UserMapper;
import com.itany.netclass.entity.GoldPoints;
import com.itany.netclass.entity.User;
import com.itany.netclass.entity.UserExample;
import com.itany.netclass.exception.EmailExistException;
import com.itany.netclass.exception.NameOrPasswordException;
import com.itany.netclass.exception.NullSringException;
import com.itany.netclass.exception.TpyeException;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.FrontMainControllerService;
import com.itany.netclass.util.MD5Util;
import com.itany.netclass.util.ParameterUtil;

import java.util.Date;
import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:21-10-20 上午11:34
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class FrontMainControllerServiceImpl implements FrontMainControllerService {
    @Override
    public User login(String username, String password) throws NameOrPasswordException {
        UserMapper userMapper = (UserMapper) ObjectFactory.getObject("UserMapper");
        List<User> list = userMapper.selectUserAndGoldByUsername(username);
        if(list.isEmpty()){
            throw new NameOrPasswordException("您的登录名不正确");
        }
        if(!list.get(0).getPassword().equals(MD5Util.md5(password))){
            throw new NameOrPasswordException("密码错误，无法登录");
        }
        User user=list.get(0);
        user.setSignDate(new Date());
        userMapper.updateByPrimaryKeySelective(user);
        return list.get(0);
    }

    @Override
    public void jianchaUsername(String username) throws NullSringException, TpyeException, EmailExistException {
        if (ParameterUtil.isNull(username)) {
            throw  new NullSringException("登录名不可为空");
        }
        UserMapper userMapper = (UserMapper) ObjectFactory.getObject("UserMapper");
        if(1!=1
//                username.matches("")
                ){
            throw new TpyeException("用户名格式不正确");
        }
        UserExample example = new UserExample();
        example.or().andLoginNameEqualTo(username);
        List<User> list = userMapper.selectByExample(example);
        if(!list.isEmpty()){
            throw new EmailExistException("给登录名已经存在请更换");
        }
    }

    @Override
    public void jianchaPassword(String password) throws NullSringException, TpyeException {
        if(ParameterUtil.isNull(password)){
            throw  new NullSringException("密码不可为空");
        }
        if(1!=1
//                password.matches("")
        ){
            throw new TpyeException("用户名格式不正确");
        }
    }

    @Override
    public void jiancharePassword(String password,String rePassword) throws NullSringException, TpyeException {
        if(ParameterUtil.isNull(password)){
            throw  new NullSringException("");
        }
        if(ParameterUtil.isNull(rePassword)){
            throw  new NullSringException("确认密码为空");
        }
        if(!password.equals(rePassword)){
            throw new TpyeException("两次密码不相同");

        }
    }

    @Override
    public void jiachaNickname(String nickname) throws NullSringException {
        if(ParameterUtil.isNull(nickname)){
            throw  new NullSringException("昵称不可以为空");
        }
    }

    @Override
    public void jianchaEmil(String email) throws TpyeException, NullSringException, EmailExistException {
        if(ParameterUtil.isNull(email)){
            throw new NullSringException("请求参数异常");

        }
        if(!email.matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$")){
            throw new TpyeException("邮箱格式错误");
        }
        UserMapper userMapper = (UserMapper) ObjectFactory.getObject("UserMapper");
        UserExample example=new UserExample();
        example.or().andEmailEqualTo(email);
        List<User> list = userMapper.selectByExample(example);
        if(!list.isEmpty()){
            throw new EmailExistException("该邮箱已经被使用，请更换");
        }
    }

    @Override
    public void addUser(String username, String password, String nickname, String email) {
        UserMapper userMapper= (UserMapper) ObjectFactory.getObject("UserMapper");
        User user =new User();
        user.setLoginName(username);
        user.setStatus(Integer.parseInt(UserConstant.USER_STATUS_QIYONG));
        user.setRole(UserConstant.USER_ROLE_PTUSER);
        user.setPassword(MD5Util.md5(password));
        user.setCreateDate(new Date());
        user.setEmail(email);
        user.setNickname(nickname);
        userMapper.insertSelective(user);
        GoldPoints goldPoints =new GoldPoints();
        goldPoints.setPointCount(Integer.parseInt(GoldPointsConstant.POINTS_CHUSHI));
        goldPoints.setGoldCount(Integer.parseInt(GoldPointsConstant.POINTS_CHUSHI));
        goldPoints.setUserId(user.getId());
        goldPoints.setCreateDate(new Date());
        GoldPointsMapper goldPointsMapper= (GoldPointsMapper) ObjectFactory.getObject("GoldPointsMapper");
        goldPointsMapper.insertSelective(goldPoints);
    }
}
